<template>
  <div class="loginwrap">
    <img src="../../assets/img/lbf.jpg" class="bg pcenter" alt="">
    <img src="../../assets/img/yd.png" class="yd" alt="">
    <div class="infop pcenter">
      <div class="st">欢迎登录,</div>
      <div class="swz">
        一粒米乡村振兴生态公园农业大数据决策分析系统
      </div>
      <div class="inputd white">
        <img src="../../assets/img/l1.png" class="small" alt="">
        <el-input v-model="farm.val.user" placeholder="请输入账号" />
      </div>
      <div class="inputd inputd2 white">
        <div class="flexdiv">
          <img src="../../assets/img/l2.png" class="small" alt="">
          <el-input v-model="farm.val.pass" :type="!passwordShow?'password':'text'" placeholder="请输入密码" />
        </div>
        <img src="../../assets/img/l4.png" @click="passwordShow=!passwordShow" class="eyepic" alt="">
      </div>
      <!-- <div class="inputd3">
        <div class="flexdiv white w1">
          <img src="../../assets/img/l3.png" class="small" alt="">
          <el-input v-model="farm.val.yzm" placeholder="请输入验证码" />
        </div>
        <div class="yzm">
          <img @click="getCode" :src="'data:image/png;base64,'+codePic" class="yzmpic" alt="">
        </div>
      </div> -->
      <div :class="[!overFlag?'overtrue':'','submit']" @click="loginGo">
        登录
      </div>
      <!-- <div class="bnotice">
        <p>Copyright © 江西鄱谷农业科技有限公司</p>
        <p class="mt-2">技术支持：北京芸播士科技有限公司、中国移动通信江西有限公司、北京大学南昌创新研究院</p>
      </div> -->
    </div>
  </div>
</template>
<script lang="ts">
import {defineComponent,onMounted,reactive,ref,computed} from 'vue'
import Setting from "@/public/config"
import Oidc from 'oidc-client'
import {post,get} from '@/public/request/api'
import {RSAEncryCode} from '@/components/_util/util'
import { useRouter } from 'vue-router';
import storage from '@/store'
import { ElMessage } from 'element-plus'
export default defineComponent({
  components: {
  },
  setup() {
    let codePic=ref('')
    let sign=''
    let num=1
    const router = useRouter()
    const config = {
      authority: Setting.Authority,
      client_id: "backend-admin",
      client_secret: "1q2w3e*",
      redirect_uri: Setting.SelfHost + Setting.Ports.BigScreen + "/user/login-callback",
      response_type: "code",
      scope: 'notification-management openid profile role offline_access account common-data big-data asset workflows visual-command internet-of-things internal-gateway hikvision identity narcissus',
      post_logout_redirect_uri: Setting.SelfHost + Setting.Ports.BigScreen + "/user/loginout"
    }
    let passwordShow=ref(false)
    let farm=reactive({
      val:{
        user:'',
        pass:'',
        yzm:'',
      }
    })
    onMounted(() => {
      //
      getCode()
    });
    let overFlag=computed({
      get(){
        return farm.val.pass&&farm.val.user
      },
      set(value){
      }
    })

    const mgr = new Oidc.UserManager(config);
    // mgr.signinRedirect().then(res => {
    //   console.log(res);
      
    // }).catch(err => {
    //   console.log(err)
    // })
    const getCode=()=>{
      get('/randomCaptcha',{}).then(res=>{
        console.log('res',res);
        
        codePic.value=res.data.base64
        sign=res.data.sign
      })
    }
    function getFarmList(){
      get('/latifundiumList',{

      }).then(res=>{
        storage.commit('changeFarmList',res.data)
        storage.commit('changeGlobalLatifundiumId',res.data[0]['latifundiumId'])
        router.push({
          path:'/user/subPage'
        })
      })
    }
    function getInfo(){
      get('/loginUserInfo',{

      }).then(res=>{
        let baseId=null
        let baseName=null
        let id=sessionStorage.getItem('nowBaseId')
        if(id){
          if(res.data.baseAuths.length>0){
            let findone=res.data.baseAuths.find(item=>{
              return item.id==id
            })
            if(findone){
              baseId=id
              baseName=findone.name
            }else{
              baseId=res.data.baseAuths[0]['id']
              baseName=res.data.baseAuths[0]['name']
            }
          }
        }else{
          if(res.data.baseAuths.length>0){
              baseId=res.data.baseAuths[0]['id']
              baseName=res.data.baseAuths[0]['name']
          }
      
        }
        storage.commit('changeUserInfo',{
          userId:res.data.userId,
          name:res.data.fullName,
          rk:res.data.roles[0]['roleKey'],
          baseId:baseId,
          baseName:baseName,
          allBase:res.data.baseAuths
        })
        storage.commit('changeGlobalLatifundiumId',res.data.userId)
        storage.commit('changeUserRole',res.data.roles[0]['roleKey'])
        // if(res.data.roles[0]['roleKey']!='admin'){
          router.push({
           path:'/user/subPage'
          })
        // }else{
        //   getFarmList()
        // }
      })
    }
    
    const loginGo=()=>{
      if(!overFlag){
        return
      }
      post('/login',{
        "code": farm.val.yzm,
        "loginType": 'pwdLogin',
        "password": RSAEncryCode(farm.val.pass),
        "sign": sign,
        "username": farm.val.user
      }).then(res=>{
        storage.commit('changeACCESS_TOKEN',res.data.token)
        storage.commit('getUandP',{
          u:farm.val.user,
          p:farm.val.pass
        })
        getInfo()
      }).catch(err=>{
        console.log('err',err)
        if(num<3){
          loginGo()
          num++
        }else{
          ElMessage.error(err.message)
          num=1
        }
      })
    }
    return { 
      passwordShow,
      farm,
      codePic,
      getCode,
      loginGo,
      overFlag
    }
  }
})
</script>
<style lang="scss" scoped>
@import '@/assets/scss/index.scss';
.eyepic{
    cursor: pointer;
  }
.loginwrap{
  width: 100vw;
  height: 100vh;
  position: relative;
  .overtrue{
    background: #ccc!important;
  }
  .yzmpic{
    width: 120px;
    height: 40px;
    cursor: pointer;
  }
  .bnotice{
    color: rgba(250, 252, 255, 1);
    margin-top: 50px;
    text-align: center;
  }
  .pcenter{
    top: 50%;
    right: 50%;
    left: 50%;
    
    bottom: 50%;
    transform: translate(-50%, -50%);
    position: absolute;
  }
  .w1{
    padding: 5px 15px;
    flex: 1;
    margin-right: 12px;
  }
  .inputd3{
    display: flex;
    align-items: center;
    justify-content: space-between;
    
  }
  .submit{
    background: rgba(24, 142, 245, 1);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 45px;
    font-size: 16px;
    color: #fff;
    margin-top: 30px;
    cursor: pointer;
  }
  
  .infop{
    background: rgba(255, 255, 255, 0.16);
    border-radius: 15px;
    padding: 40px 15px;
    width:535px;
    height: 400px;
    z-index: 99;
    right: 10%!important;
    left: 82%;
    .flexdiv{
      display: flex;
      align-items: center;
    }
    .inputd{
      display: flex;
      align-items: center;
      padding: 5px 15px;
      margin-bottom: 15px;
    }
    .st{
      color: #fff;
      font-size: 16px;
      margin-bottom: 10px;
    }
    .inputd2{
      justify-content: space-between;
    }
    .white{
      background: #fff;
      border-radius: 8px 8px 8px 8px;
      opacity: 1;
      border: 1px solid #188EF5;
    }
  }
  .swz{
   
    background: linear-gradient(180deg, #FFFFFF 0%, #0E9AE9 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 21px;
    font-weight:bold;
    margin-bottom: 40px;
  }
  .bg{
    position: absolute;
   
    width: 100%;
   
    
  }
  .yd{
    width: 200px;
    position: fixed;
    right: 1.5%;
    bottom: 3%;
  }
  
}
</style>